﻿import QtQuick 2.5
import QtQuick.Controls 1.4
import QtQuick.Dialogs 1.2
import QtSensors 5.3
import QtQuick.Window 2.2
import QtQuick.Controls.Styles 1.4


ApplicationWindow {
    visible: true
    width: Screen.width
    height: Screen.height
    title: qsTr("萌梦大作战")

    property real screenWidth: Screen.width
    property real screenHeight: Screen.height

    property real ballRadius: 500

    property real ballCenterX: 0
    property real ballCenterY: 0

    property var colors: ["gray", "red", "yellow", "green"]

    property int score: 0

    property bool isGameOver: false

    property int maxScore: 0

    property int lifeValue: 3

    property var items: new Array


    Rectangle{
        id: mengRect
        width: imgMeng.width
        height: imgMeng.height
        anchors.horizontalCenter: parent.horizontalCenter
        anchors.bottom: parent.bottom
        color: Qt.rgba(0,0,0,0)
        border.width: 5
        border.color: {
            if(lifeValue>=0){
                colors[lifeValue]
            }
            else{
                "gray"
            }
        }

        AnimatedImage{
            id: imgMeng
            source: "qrc:/img/img/m.gif"
        }
    }

    Item{
        id: itemGhost
        anchors.fill: parent
    }

    Button{
        anchors.right: parent.right
        text: "提示"
        z: 1
        style: ButtonStyle{}
        onClicked: {
            messageDialog.visible = true
        }
    }

    MessageDialog{
        id: messageDialog
        visible: false
        title: "游戏提示"
        informativeText: "消灭幽灵，保护萌梦！"
    }

    MessageDialog{
        id: messageDialogRestart
        visible: false
        title: "萌梦大作战"
        informativeText: "重新开始？"
        onAccepted: {
            //清除所有ghost
            for(var i in items){
                items[i].destroy()
            }
            items.length = 0

            score = 0
            lifeValue = 3
            isGameOver = false
            timerGame.restart()
            timer.restart()
        }
    }

    //生命值
    Row{
        id: itemHead
        spacing: 100
        x: 10
        y: 10
        z: 1

        Row{
            id: row
            width: 130*3+spacing*2
            height: imgHeart1.height
            spacing: 5

            Image{
                id: imgHeart1
                source: lifeValue>0?"qrc:/img/img/heart.png":"qrc:/img/img/heart2.png"
            }
            Image{
                source: lifeValue>1?"qrc:/img/img/heart.png":"qrc:/img/img/heart2.png"
            }
            Image{
                source: lifeValue>2?"qrc:/img/img/heart.png":"qrc:/img/img/heart2.png"
            }
        }

        Text{
            id: scoreText
            anchors.verticalCenter: row.verticalCenter
            text: "分数：" + String(score)
            font.pixelSize: 50
            font.family: "微软雅黑"
        }
    }


    //生成ghost
    Timer{
        id: timer
        interval: 2000
        triggeredOnStart: true
        repeat: true
        running: true
        onTriggered: {

            var s = getSeed()
            Math.seed = s

            if(Math.random()>0.5){
                Math.seed = String(s).split("").reverse().join("")
            }
//            console.log(Math.seed)

            var ghost = Qt.createComponent("Ghost.qml").createObject(itemGhost)

            var x = Math.floor(Math.random()*(500))
            ballCenterY = Math.floor(Math.random()*(screenHeight-mengRect.height+500)) - 500

            if(x < 500/2){
                ballCenterX = -x
            }
            else{
                ballCenterX = x + screenWidth
            }

            ghost.x = ballCenterX - ghost.width/2
            ghost.y = ballCenterY - ghost.height/2

            var mengX = screenWidth/2
            var mengY = screenHeight - mengRect.height/2

            var rad = Math.atan2(ballCenterY-mengY,ballCenterX-mengX)

            var angle = 180 / Math.PI * rad

            var sp = Math.sqrt((ballCenterY-mengY)*(ballCenterY-mengY) + (ballCenterX-mengX)*(ballCenterX-mengX))

//            console.log(rad, angle, sp, ghost.x, ghost.y)

            if (ballCenterX <= 0){
                //从左侧出来
                ghost.type = "ghost2"
                angle = angle + 180
            }

            ghost.rotation.angle = angle
            ghost.sp = sp
            ghost.animMoving.restart()

            items.push(ghost)

        }
    }

    Timer{
        id: timerGame
        interval: 30
        repeat: true
        running: true
        onTriggered: {
            if(funcIsGameOver()){
                isGameOver = true
                timerGame.stop()
                timer.stop()
                maxScore = Math.max(maxScore, score)

                //停止所有ghost
                for(var i in items){
                    items[i].pause()
                }

                messageDialogRestart.visible = true
            }
        }
    }

    function deleteObjFromItems(obj){
        for(var i in items){
            if(obj == items[i]){
                obj.destroy()
                items.splice(i, 1)
                break
            }
        }
//        console.log(items.length)
    }

    function funcIsGameOver(){
        //检测是否被球碰到，死亡

        if(lifeValue <= 0){
            return true
        }
        return false
    }

    function addZero(value){
        if(value < 10){
            return "0" + value
        }
        return value
    }

    function getSeed(){

        var date = new Date
        var y = date.getFullYear()
        var M = addZero(date.getMonth())
        var d = addZero(date.getDate())
        var h = addZero(date.getHours())
        var m = addZero(date.getMinutes())
        var s = addZero(date.getSeconds())

        var time = String(y) + String(M) + String(d) + String(h) + String(m) + String(s)

        return Number(time)
    }

}

